package cn.keepme.ep.member.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.jweb.core.bean.ReplyDataMode;
import org.jweb.core.util.Md5Util;
import org.jweb.core.util.ReplyCodeResourceUtil;
import org.jweb.core.util.ResourceUtil;
import org.jweb.core.web.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.keepme.ep.common.service.SMSServiceI;
import cn.keepme.ep.common.service.SysServiceI;
import cn.keepme.ep.member.entity.GeneralUserEntity;
import cn.keepme.ep.util.MemberUtil;

/**
 * 会员密码管理控制器
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/member/pwd")
public class PwdController extends BaseController {
	@Autowired
	private SysServiceI sysService;
	@Autowired
	private EhCacheManager ehcacheCacheManager;
	@Autowired
	private SMSServiceI smsService;
	/**
	 * 发送短信验证码
	 * @param loginForm
	 * @return
	 */
	@RequestMapping(value = "/sendSMS")
	@ResponseBody
	public ReplyDataMode sendSMS(String mphone,HttpServletRequest req){
		
		return MemberUtil.sendSMSValidCode(mphone, req.getLocale(), smsService, ehcacheCacheManager);
	}
	
	/**
	 * 修改密码
	 * @param loginForm
	 * @return
	 */
	@RequestMapping(value = "/modify",method=RequestMethod.POST)
	@ResponseBody
	public ReplyDataMode modify(String pwd,String smsCode,String mphone){
		ReplyDataMode j = new ReplyDataMode();
		if(	!MemberUtil.validSMSCode(mphone, smsCode, ehcacheCacheManager)){
			j.setSuccess(false);
			j.setStatusCode(ReplyCodeResourceUtil.getProperties("regist_validCode_error"));
			return j;
		}
	
		/**修改密码*/
		String defaultEhcacheName = ResourceUtil.getConfigByName("defaultEhcacheName");
		GeneralUserEntity user = (GeneralUserEntity)sysService.findUniqueByProperty(GeneralUserEntity.class, "mphone", mphone);
		user.setPwd(Md5Util.getMD5(user.getPwd()));
		this.ehcacheCacheManager.getCache(defaultEhcacheName).remove(mphone);//从缓存服务器中清除短信验证码信息
		sysService.updateEntitie(user);
		
		
		j.setSuccess(true);
		return j;
	}
}
